package day23;

// 单模式之懒汉式的线程安全问题
public class Demo07 {
	public static void main(String[] args) {
		Runnable run = new Runnable() {
			@Override
			public void run() {
				Single s = Single.getInstance();
				System.out.println(s.hashCode());
			}
		};

		new Thread(run).start();
		new Thread(run).start();
		new Thread(run).start();
		new Thread(run).start();

	}
}

// ------------------

class Single {
	private Single() {
	}

	private static Single instance;

	public static Single getInstance() {
		// 锁外面的判断是为了提高效率
		if (instance == null) {
			synchronized (Single.class) {
				// 锁里面的判断, 是为了确保对象的单例性
				if (instance == null) {
					instance = new Single();
				}
			}
		}
		return instance;
	}

}